package com.ruoyi.web.course;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.course.vo.SCourseVo;
import com.ruoyi.course.service.ISCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 学生课程Controller
 *
 * @author ruoyi
 * @date 2024-12-03
 */
@Api(tags = "学生课程Controller")
@RestController
@RequestMapping("/course/s_course")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@CrossOrigin(origins = "http://localhost:8080")
public class SCourseController extends BaseController {

    private final ISCourseService sCourseService;

    @ApiOperation("查询学生课程列表")
    @PreAuthorize("@ss.hasPermi('course:s_course:list')")
    @GetMapping("/list")
    public TableDataInfo<SCourseVo> list(SCourseVo entity) {
        return sCourseService.queryList(entity);
    }

    @ApiOperation("查询学生课程所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(SCourseVo entity) {
        return AjaxResult.success("查询成功", sCourseService.queryAll(entity));
    }

    @ApiOperation("导出学生课程列表")
    @PreAuthorize("@ss.hasPermi('course:s_course:export')")
    @Log(title = "学生课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SCourseVo entity) {
        List<SCourseVo> list = sCourseService.queryAll(entity);
        ExcelUtil<SCourseVo> util = new ExcelUtil<>(SCourseVo.class);
        util.exportExcel(response, list, "学生课程数据");
    }

    @ApiOperation("获取学生课程详细信息")
    @PreAuthorize("@ss.hasPermi('course:s_course:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", sCourseService.queryById(id));
    }

    @ApiOperation("新增学生课程")
    @PreAuthorize("@ss.hasPermi('course:s_course:add')")
    @Log(title = "学生课程", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody SCourseVo entity) {
        return toAjax(sCourseService.save(entity));
    }

    @ApiOperation("修改学生课程")
    @PreAuthorize("@ss.hasPermi('course:s_course:edit')")
    @Log(title = "学生课程", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody SCourseVo entity) {
        return toAjax(sCourseService.updateById(entity));
    }

    @ApiOperation("删除学生课程")
    @PreAuthorize("@ss.hasPermi('course:s_course:remove')")
    @Log(title = "学生课程", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sCourseService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
